<?
class attachment{
	private 
		$tbl = 'attachments';
	const 
		ATT_IMG = 0,
		ATT_FILE = 1;

	var	
		$id,
		$type,         	#image=0,file=1
		$parent_id,
		$parent_type,		#materials
		$filename;
	var
		$files = array(),
		$base_dir = "",
		$sizes = array();

	function __construct($param=NULL,$base_dir=""){
		global $db;
		$base_dir = rtrim($base_dir,'/').'/';		#siempre se termina con barra 
		if (!$param){
			$this->id = 0;
			$this->type = 0;
			$this->parent_id = 0;
			$this->parent_type = "";
			$this->filename = "";
			$this->base_dir = $base_dir;
		}elseif(is_array($param)){
			$this->id = (int)$param['id'];
			$this->type = $param['type'];
			$this->parent_id = (int)$param['parent_id'];
			$this->parent_type = $param['parent_type'];
			$this->filename = $param['file'];
			$this->base_dir = $base_dir;
		}elseif(is_int($param)){
			$sql = "
				SELECT
					id,
					type,
					parent_id,
					parent_type,
					file,
					IS_NULL(original_name,file,original_name) original_name
				FROM
					$this->tbl
				WHERE
					id = $param
				LIMIT 1
			";
			$row = $db->query_line($sql);
			if ($row){
				$this->id = (int)$row['id'];
				$this->type = $row['type'];
				$this->parent_id = (int)$row['parent_id'];
				$this->parent_type = $row['parent_type'];
				$this->filename = $row['file'];
				$this->original_name = $row['original_name'];
				$this->base_dir = $base_dir;
			}else{
				$this->id = 0;
				$this->type = 0;
				$this->parent_id = 0;
				$this->parent_type = "";
				$this->filename = "";
				$this->original_name = "";
				$this->base_dir = $base_dir;
			}
		}
	}
	
	function is_image(){
		return $this->type == ATT_IMG;
	}

	function is_file(){
		return $this->type == ATT_FILE;
	}
	
	function ext($filename=''){
		$filename = empty($filename) ? $this->filename : $filename;
		$return = substr(strrchr($filename, '.'), 1);
		return $return;
	}
	
	function file_path($filename=""){
		$return = $this->base_dir.(empty($filename) ? $this->filename : $filename);
		return $return;
	}
	
	function exists(){
		return file_exists($this->file_path());
	}
	function no_file(){
		return !$this->exists();
	}
	
	/*
		se puede llamarla de dos maneras:
		get_image('icon')
		o
		get_image(array('width'=>150,'height'=>150,'quality'=>90,'crop'=>false));
	*/
	function get_image($size = '_original_'){
		$return = false;
		
		if (is_string($size) && $size=='_original_'){
			$return = $this->file_path();
		}else{
			if (is_string($size) && !empty($this->sizes[$size])){
				$size = $this->sizes[$size];
			}elseif(is_array($size)){
//				$size = $size;
			}
		
			if (isset($size['width']) || isset($size['height'])){
				$resized_file = $this->file_path('cache/'.$this->get_filename($size));
				if (file_exists($resized_file) || image_resize($this->file_path(),$resized_file,$size)){
					$return = resized_file;
				}
			}
		}
		return $return;
	}
	
	function add_image($file,$forse=false){
		$return = false;
		$this->type = ATT_IMG;
		$return = $this->add_attachment($file,$forse);
		return $return;
	}

	function add_file($file,$forse=false){
		$return = false;
		$this->type = ATT_FILE;
		$return = $this->add_attachment($file,$forse);
		return $return;
	}
	
	function delete(){
		global $db;
		$return = false;
		$sql = "
			DELETE FROM 
				$this->tbl
			WHERE 
				id = ".(int)$this->id."
			LIMIT 1
		";
		if ($db->query($sql)){
			$return = true;
			if (!unlink($this->file_path())){
				add_event('file_delete_fail',"No se ha podido eliminar el archivo '$this->base_dir.$this->file'",get_class($this));
			}
			if (!unlink($this->file_path('cache/'.$this->filename.'.*'))){
				add_event('file_delete_cache_fail',"No se ha podido eliminar el cache del archivo '$this->base_dir.$this->file'",get_class($this));			
			}
		}
		return $return;
	}
	
	private function get_filename($size){
		$return = @$this->file.".{$this->id}_{$size[width]}x{$size[height]}_{$size[crop]}.".$this->ext();
	}
	
	/*
		$file se pasa en el formato:
			$file['name']		= ...
			$file['tmp_name']	= ...
			$file['size']		= ...
			$file['error']		= ...
	*/
	private function add_attachment($file,$forse=false){
		$return = false;
		mkdir($this->base_dir,0777,true/*recursive*/);
		$this->file = basename($file['tmp_name']).$this->ext($file['name']);
		
		$i = 0;
		if (	
				(	
					$forse && 
					copy($file['tmp_name'],$this->base_dir.$this->file)
				)
			||
				(
					is_uploaded_file($file['tmp_name']) && 
					move_uploaded_file($file['tmp_name'],$this->base_dir.$this->file)
				)
		){
			chmod($this->base_dir.$this->file,0777);
			$sql = "
				INSERT INTO 
					$this->attachments
					(type,parent_id,parent_type,file,original_name)
				VALUES
					($this->type,$this->parent_id,'".block($this->parent_type)."','".block($this->file)."','".block($file['name'])."')
			";
			if ($db->query($sql)){
				$this->id = (int)$db->insert_id();
				$new_name = $this->parent_id."_".$this->id.'.'.$this->ext($file['name']);
				if (rename($this->file,$new_name)){
					$sql = "
						UPDATE
							$this->attachments
						SET
							file = '".block($new_name)."'
						WHERE
							id = '$this->id'
					";
					if ($db->query($sql)){
						$this->name = $new_name;
						$return = true;
					}
				}else{
					add_event('file_upload_bad_name',"No se ha podido renombrar el archivo '$this->base_dir.$this->file' a '$new_name'",get_class($this));
				}
			}
			$return = true;
		}
		return $return;
	}

}
?>